package code.ws_common_scene.command.cmd
{
	import code.ws_common_scene.command.Command;
	import code.ws_common_scene.manager.SceneManager;
	import code.ws_common_scene.map.MapModePool;
	import code.ws_common_scene.map.XMapMode;
	import code.ws_common_scene.mode.Trans;
	
	import flash.geom.Point;

	/**
	 * 传送阵指令 
	 * @author qiangchen
	 * 
	 */	
	public class TransCommand extends Command
	{
		/** 显示对象 */
		private var _obj:Trans;
		
		/**
		 * 构造函数 
		 * @param transObj	传送阵对象
		 * @param distance 距传送阵对象多少距离触发完成方法（默认：1个格子距离）
		 * 
		 */		
		public function TransCommand( transObj:Trans )
		{
			super( null, false );
			_obj = transObj;
		}
		
		
		/**
		 * 执行指令
		 * 
		 */		
		override public function execute():void
		{
			var tar_p:Point = _obj.tiles;
			var hero_p:Point = SceneManager.instance.scene.hero.tiles;
			if( tar_p.equals( hero_p ) )
			{
				tar_p = hero_p = null;
				over();
				return;
			}
			
			var path:Array = SceneManager.instance.scene.findPath( hero_p, tar_p );
			if( path == null || path.length == 0 )
			{
				tar_p = hero_p = null;
				if( _innerCallFn != null )
				{
					_innerCallFn.call( null );
				}
				dispose();
				return;
			}
			
			// 寻路至目的地
			SceneManager.instance.scene.hero.walk( path.slice() );
			tar_p = hero_p = null;
			path.length = 0;
			path = null;
		}
		
		
		/**
		 * 指令结束  
		 * 
		 */		
		override public function over():void
		{ 
			// 请求传送场景
			trace("【ws_mobile】---> send scene transfer! trans mapId: " + _obj.nextMapId);
			
			var xmapMode:XMapMode = MapModePool.getMapMode( SceneManager.instance.scene.mapId );
			SceneManager.instance.scene.setEnterMapPos( xmapMode._transInfos[_obj.nextMapId].transPos );
			xmapMode = null;
			SceneManager.instance.enterMap( _obj.nextMapId );
			
			_obj = null;
			super.over();
		}
		
		
		/**
		 * 释放指令 
		 * 
		 */		
		override public function dispose():void
		{
			_obj = null;
			super.dispose();
		}
	}
}